package com.ndpmedia.fb.pmd.configration.db;

import com.ndp.fb.util.DateUtil;
import com.ndp.fb.util.StringUtil;
import com.ndpmedia.fb.pmd.configration.model.po.PropertiesPo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Mikey.zhaopeng on 2014/10/12 20:47.
 * Email mikey.zhao@ndpmedia.com
 */
@Repository
public class PropertiesRepository extends BasePepository {


    @Autowired
    private JdbcTemplate jdbcTemplate;

    public List findAll(String appid, String configid) {
        String sql = "select * from properties where appid='"+appid+"' and configid='"+configid+"'";
        return queryForListObject(sql, PropertiesPo.class);
    }

    public PropertiesPo save(final String appid, final String configid, final String key, final String value,final String description) {


        final  String sql = "INSERT INTO properties(APPID, CONFIGID,`KEY`,VALUE,DESCRIPTION,UPDATE_TIME) VALUES(?,?,?,?,?,?)";
        KeyHolder keyHolder = new GeneratedKeyHolder();
        jdbcTemplate.update(new PreparedStatementCreator() {
            public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
                PreparedStatement ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
                ps.setString(1, appid);
                ps.setString(2, configid);
                ps.setString(3, key);
                ps.setString(4, value);
                ps.setString(5, description);
                ps.setString(6, DateUtil.getCurrDate(DateUtil.FORMAT_ONE));
                return ps;
            }
        }, keyHolder);

        Integer id = keyHolder.getKey().intValue();

        PropertiesPo properties = new PropertiesPo(id,appid,configid,key,value);

        return properties;
    }


    public boolean delete(String propertiesid) {
        Map params = new HashMap();
        params.put("propertiesid", propertiesid);
        String sql = "delete from properties where id=?";
        getLogInfo(sql, params);
        int rc = this.jdbcTemplate.update(sql, propertiesid);
        return true;
    }

    public boolean deleteByappId(String appId) {
        Map params = new HashMap();
        params.put("appId", appId);
        String sql = "delete from properties where appid=?";
        getLogInfo(sql, params);
        int rc = this.jdbcTemplate.update(sql, appId);
        return true;
    }

    public boolean update(String propertiesid, String key, String value,String description) {
        String sql = "update properties set ";
        Map params = new HashMap();
        params.put("id", propertiesid);
        String data = "";
        if(StringUtil.isNotEmpty(value)){
            params.put("value", value);
            sql = sql+" value=? ";
            data = value;
        }else if(StringUtil.isNotEmpty(key)){
            params.put("key", key);
            sql = sql+" `key`=? ";
            data = key;
        }else if(StringUtil.isNotEmpty(description)){
            params.put("description", description);
            sql = sql+" `description`=? ";
            data = description;
        }
        sql = sql+ " where id=?";

        getLogInfo(sql, params);
        int rc = this.jdbcTemplate.update(sql, data,propertiesid);
        return true;
    }
}
